package com.zhaodui.agora;

import com.xiaoleilu.hutool.http.HttpUtil;
import com.zhaodui.mq.entity.FxjMqLogEntity;
import com.zhaodui.util.alioss.AliyunOSSClientUtil;
import net.sf.json.JSONObject;
import org.apache.tools.ant.taskdefs.Sleep;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.JSONHelper;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class agoraUtil {

    public  static  String startRec(String channel){
        String sid="";
        String url = "http://www.ebaokeji.cn:3000/recorder/v1/start";
//        try{
//            url = ResourceUtil.getConfigByName("agroa.url")+"start";
//        }catch (Exception e){
//
//        }
        String appid="5547c404c0e24b42b2b3d46cad586526";
        Map<String ,Object> map = new HashMap<>();
        map.put("appid",appid);
        map.put("channel",channel);
        String body="";
        body = JSONHelper.map2json(map);
        String res  = HttpUtil.post(url,body);
        try{
            JSONObject resultJson = JSONObject.fromObject(res);
            sid = String.valueOf(resultJson.get("sid"));
        }catch (Exception e){

        }

        return  sid;

    }


    public  static  String stopRec(String sid){
        String status = "false";
        String url = "http://www.ebaokeji.cn:3000/recorder/v1/stop";
//        try{
//            url = ResourceUtil.getConfigByName("agroa.url")+"stop";
//        }catch (Exception e){
//
//        }
        String appid="5547c404c0e24b42b2b3d46cad586526";
        Map<String ,Object> map = new HashMap<>();
        map.put("sid",sid);
        String body="";
        body = JSONHelper.map2json(map);
        String res  = HttpUtil.post(url,body);
        try{
            JSONObject resultJson = JSONObject.fromObject(res);
            status = String.valueOf(resultJson.get("success"));
        }catch (Exception e){
        }
        try {
            TimeUnit.SECONDS.sleep(60);//秒
                   } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try{
            String[] arguments = new String[] { "python", "/mydata/videosoft/record/src/sdk/tools/video_convert.py", "-f", "/mydata/videosoft/server/output/"+sid };
            try {
                Process process = Runtime.getRuntime().exec(arguments);
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                int re = process.waitFor();
                System.out.println(re);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){

        }
        try {
            TimeUnit.SECONDS.sleep(60);//秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try{
            String tofilepath =  "/mydata/videosoft/tmp/";
            File file = new File("/mydata/videosoft/server/output/"+sid);
            System.out.print("file"+file.length());

            if (file.exists()) {
                File[] files = file.listFiles();
                if (files.length == 0) {

                } else {
                    for (File file2 : files) {
                        System.out.print("file2.getAbsolutePath()"+file2.getAbsolutePath());

                        if (file2.isDirectory()) {
                        } else {
                            if(file2.getAbsolutePath().indexOf("av")>0){
                            System.out.print("AVAVAVA****"+file2.getAbsolutePath());
                                try{
                                   FileCopyUtils.copy(new FileInputStream(file2.getAbsolutePath()), new FileOutputStream(tofilepath+sid+".mp4"));
                                    AliyunOSSClientUtil t = new AliyunOSSClientUtil();
                                    String urlmp4 = t.uploadvideo(tofilepath+sid+".mp4");
                                    urlmp4 = "https://zhaodui.oss-cn-shenzhen.aliyuncs.com/"+urlmp4 ;
                                    return urlmp4;
                                }catch (Exception e){
                                    FileCopyUtils.copy(new FileInputStream(file2.getAbsolutePath()), new FileOutputStream(tofilepath+sid+".mp4"));
                                    AliyunOSSClientUtil t = new AliyunOSSClientUtil();
                                    String urlmp4 = t.uploadvideo(tofilepath+sid+".mp4");
                                    urlmp4 = "https://zhaodui.oss-cn-shenzhen.aliyuncs.com/"+urlmp4;
                                    return urlmp4;
                                }
                            }
                        }
                    }
                }
            }

            }catch (Exception e2){

            }


        return  status;

    }

}
